package com.imm.service.impl;

import com.alibaba.druid.sql.visitor.functions.Lcase;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imm.common.CustomException;
import com.imm.common.Result;
import com.imm.mapper.CategoryMapper;
import com.imm.pojo.Category;
import com.imm.pojo.Dish;
import com.imm.pojo.Setmeal;
import com.imm.service.ICategoryService;
import com.imm.service.IDishService;
import com.imm.service.ISetmealService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Resource
    private IDishService dishService;

    @Resource
    private ISetmealService setmealService;

    /**
     * 分页查询
     * @param page 页数
     * @param pageSize 页大小
     * @return 响应
     */
    @Override
    public Result<Page<Category>> pageCategory(int page, int pageSize) {
        Page<Category> pageInfo =  new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<Category>();
        wrapper.orderByAsc(Category::getSort);
        page(pageInfo, wrapper);
        return Result.success(pageInfo);
    }

    /**
     * 新增分类
     * @param category 分类实体类
     * @return 响应
     */
    @Override
    public Result<String> insert(Category category) {
        save(category);
        return Result.success("添加成功");
    }

    @Override
    public Result<String> delete(Long ids) {
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId, ids);
        LambdaQueryWrapper<Setmeal> setWrapper = new LambdaQueryWrapper<>();
        setWrapper.eq(Setmeal::getCategoryId, ids);
        // 查看是否关联套餐
        List<Dish> dishes = dishService.list(dishWrapper);
        List<Setmeal> setmeals = setmealService.list(setWrapper);
        if (!dishes.isEmpty() || !setmeals.isEmpty()) {
            throw new CustomException("该分类已被关联，不能删除");
        }
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Category::getId, ids);
        remove(wrapper);
        return Result.success("删除成功");
    }

    @Override
    public Result<String> updateCategory(Category category) {
        updateById(category);
        return Result.success("修改成功");
    }

    @Override
    public Result<List<Category>> listType(Category category) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(category.getType() != null, Category::getType, category.getType())
                .orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = list(wrapper);
        return Result.success(list);
    }
}
